package com.shd.edanpei.customer.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.shd.edanpei.base.data.RxBusCode;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.ui.activity.ChoosePicActivity;
import com.shd.edanpei.R;
import com.shd.edanpei.ui.activity.PayServicePresenter;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.rxbus.RxBus;
import com.shd.edanpei.bean.PayServiceModel;
import com.shd.edanpei.presenter.contract.PayServiceContract;
import com.shd.edanpei.enums.StatusEnum;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.ui.activity.SureLoanInfoActivity;
import com.shd.edanpei.ui.view.PlanLineView;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by mc on 2018/9/28.
 * yang-wen-wen
 */

public class PayServiceActivity extends BaseActivity<PayServicePresenter> implements PayServiceContract.PayServiceIView {

    private final int PHOTO = 101;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.planLine_view)
    PlanLineView planLine;
    @BindView(R.id.tv_bank)
    TextView tvBank;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_amount)
    TextView tvAmount;
    @BindView(R.id.iv_pic)
    ImageView ivPic;
    @BindView(R.id.rr_warning)
    RelativeLayout rrWarning;
    @BindView(R.id.iv_dalete)
    ImageView ivDatale;

    private int status;
    private int orderID;

    @Override
    protected int setLayout() {
        return R.layout.activity_payservice;
    }

    @Override
    protected PayServicePresenter bindPresenter() {
        return new PayServicePresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        RxBus.get().register(this);
        loadImageCtrl = new LoadImageCtrl(this);
        headline.setText("支付服务费");
        Bundle bundle = getIntent().getExtras();
        orderID = bundle.getInt(Constant.ORDER_ID);
        status = bundle.getInt(Constant.TYPE);
        mPresenter.getData(orderID);
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_dalete)
    void dalate(){
        rrWarning.setVisibility(View.GONE);
    }

    @OnClick(R.id.iv_headline_back)
    void close() {
        finish();
    }

//    @OnClick(R.id.ll_info)
//    void onInfo() {
//        Bundle bundle = new Bundle();
//        bundle.putInt(Constant.ORDER_ID, orderID);
//        startActivity(SureLoanInfoActivity.class, bundle);
//    }

    @OnClick(R.id.iv_pic)
    void onPic() {
        ChoosePicActivity.start(this, PHOTO);
    }


    @Override
    public void setData(PayServiceModel model) {
        tvBank.setText(model.getOpenBank());
        tvName.setText(model.getAccountName());
        tvAccount.setText(model.getAccountNo());
        tvAmount.setText(model.getServiceAmount());

        if (!TextUtils.isEmpty(model.getServiceChargeCert())) {
            loadImageCtrl.getGlide().load(model.getServiceChargeCert()).into(ivPic);
        }
//
//        if (!TextUtils.isEmpty(model.getBankCard())) {
//            doneAuditData();
//        }

        if (TextUtils.isEmpty(model.getServiceChargeCert())) {
            //未转账
            planLine.setData(100);
        } else if (!TextUtils.isEmpty(model.getServiceChargeCert()) && TextUtils.isEmpty(model.getBankCard())) {
            planLine.setData(101);
        } else if (!TextUtils.isEmpty(model.getServiceChargeCert()) && !TextUtils.isEmpty(model.getBankCard()) && status != StatusEnum.success.getStatus()) {
            planLine.setData(102);
        } else if (status == StatusEnum.success.getStatus()) {
            planLine.setData(103);
        }
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //编辑返回
        if (requestCode == ChoosePicActivity.INTENT_REQUEST_CODE) {
            if (null != data) {
                switch (resultCode) {
                    case PHOTO:
                        String picturePath = data.getStringExtra(ChoosePicActivity.INTENT_BACK_DATA);
                        loadImageCtrl.getGlide().load(picturePath).into(ivPic);
                        mPresenter.upload(picturePath, orderID);
                        DialogUtil.showDialog4(getContext(), new DialogUtil.SureListener() {
                            @Override
                            public void sure() {
                                RxBus.get().send(RxBusCode.UPDATA_PHOTO_SUCCESS);
                                getContext().finish();
                            }
                        }, "提示", "凭证上传成功，待审核", "返回列表");
                        break;
                }
            }
        }

    }


    @Override
    protected void onDestroy() {
        RxBus.get().unRegister(this);
        super.onDestroy();
    }
}
